import config from '../../../src/config'

function genPie(){
  $.ajax({
       type: "GET",
       url: config.serverURI+'/demo/app/distribution',
       data: '',
       dataType: "json",
       success: function(res){
         // console.log( res.aggregations[2].buckets );
         var data = [];
         var arr = [];
         arr[1] = "衣快链";
         arr[3] = "衣布到位";
         arr[7] = "分销资源计划系统DRP";

         res.aggregations[2].buckets.forEach(function (item, key) {
           var subdata = {};
           subdata.value = item.doc_count;
           subdata.name = arr[item.key];
           if(subdata.value > 1)
            data.push(subdata);
         });

         data = data.sort(function (a, b) { return a.value - b.value; });

         var option = {
            // backgroundColor: '#fcf4fc',

            title: {
                text: 'APP登陆次数分布',
                left: 'center',
                top: 20,
                textStyle: {
                    color: '#ccc'
                }
            },

            tooltip : {
                trigger: 'item',
                formatter: "{a} <br/>{b} : {c} ({d}%)"
            },

            visualMap: {
                show: false,
                min: 100,
                max: 3000,
                inRange: {
                    colorLightness: [0, 1]
                }
            },
            legend: {
                orient: 'vertical',
                left: 'left',
                data:
                data.map(function (item) {
                  return item.name;
                })
            },
            series : [
                {
                    name:'访问来源',
                    type:'pie',
                    radius : '55%',
                    center: ['50%', '60%'],
                    data: data,
                    // roseType: 'radius',
                    // label: {
                    //     normal: {
                    //         textStyle: {
                    //             color: 'rgba(255, 255, 255, 0.3)'
                    //         }
                    //     }
                    // },
                    // labelLine: {
                    //     normal: {
                    //         lineStyle: {
                    //             color: 'rgba(255, 255, 255, 0.3)'
                    //         },
                    //         smooth: 0.2,
                    //         length: 10,
                    //         length2: 20
                    //     }
                    // },
                    itemStyle: {
                        emphasis: {
                          shadowBlur: 10,
                          shadowOffsetX: 0,
                          shadowColor: 'rgba(0, 0, 0, 0.5)'
                      }
                    },

                    // animationType: 'scale',
                    // animationEasing: 'elasticOut',
                    // animationDelay: function (idx) {
                    //     return Math.random() * 200;
                    // }
                }
            ]
        };
         var chart2 = echarts.init(document.getElementById('disPie'));
         chart2.setOption(option);
      }
   });
}

export { //很关键
  genPie
}
